<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

if (!function_exists('cm_flash_message')) {

    function cm_flash_message($type, $message)
    {
        $data = array();
        $CI = & get_instance();
        $data['message'] = $message;

        switch ($type) {
            case 'success':
                $data['type'] = 'alert-success';
                break;
            case 'error':
                $data['type'] = 'alert-error';
                break;
        }
        return $data;
    }

}

if (!function_exists('admin_url')) {

    function admin_url($uri = '')
    {
        $CI = & get_instance();
        $uri = 'admin/' . $uri;
        return $CI->config->site_url($uri);
    }

}

if (!function_exists('redirect_admin')) {

    function redirect_admin($uri = '', $method = 'location', $http_response_code = 302)
    {
        if (!preg_match('#^https?://#i', $uri)) {
            $uri = admin_url($uri);
        }

        switch ($method) {
            case 'refresh' : header("Refresh:0;url=" . $uri);
                break;
            default : header("Location: " . $uri, TRUE, $http_response_code);
                break;
        }
        exit;
    }

}

if (!function_exists('config_pagination')) {

    function config_pagination($url, $total, $segment)
    {
        global $CFG;
        $config = array();
        $config ['full_tag_open'] = '<div class="pagination">';
        $config ['full_tag_close'] = '</div>';
        $config ['first_tag_open'] = '';
        $config ['first_tag_close'] = '&nbsp;';
        $config ['last_tag_open'] = '&nbsp;';
        $config ['last_tag_close'] = '';
        $config ['first_url'] = ''; // Alternative URL for the First Page.
        $config ['cur_tag_open'] = '<li class="active"><a href="#">';
        $config ['cur_tag_close'] = '</a></li>';
        $config ['next_tag_open'] = '<li>';
        $config ['next_tag_close'] = '</li>';
        $config ['prev_tag_open'] = '<li>';
        $config ['prev_tag_close'] = '</li>';
        $config ['num_tag_open'] = '<li>';
        $config ['num_tag_close'] = '</li>';
        $config['base_url'] = $url;
        $config['total_rows'] = $total;
        $config['per_page'] = $CFG->item('per_page');
        $config['uri_segment'] = $segment;
        return $config;
    }

}

/*
 * @todo: upload file images
 */
if (!function_exists('doUpload')) {

    function doUpload($fieldname = '', $config = array())
    {
        if (isset($_FILES[$fieldname]['name']) && !empty($_FILES[$fieldname]['name'])) {
            $CI = & get_instance();
            $newFileName = $_FILES[$fieldname]['name'];
            $fileExt = array_pop(explode(".", $newFileName));
            $filename = md5(time()) . "." . $fileExt;
            $config['file_name'] = $filename;
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['max_size'] = '100';
            $config['max_width'] = '1024';
            $config['max_height'] = '768';
            $CI->load->library('upload', $config);

            if (!$CI->upload->do_upload($fieldname)) {
                $error = array('error' => $CI->upload->display_errors());
                return $error;
            } else {
                $data = array('upload_data' => $CI->upload->data());
                return $data;
            }
        }
        return NULL;
    }

}

/*
 * @todo: display image on browser
 */
if (!function_exists('image_url')) {

    function image_url($path = '')
    {
        ini_set('display_errors', 0);
        $file = base64_decode($path);
        $infos = getimagesize($file);
        $mimeType = $infos['mime'];
        $expires = 60 * 60 * 24 * 14;
        header("Pragma: public");
        header("Cache-Control: maxage=" . $expires);
        header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');

        if (file_exists($file)) {
            header("Content-type: $mimeType");
            echo file_get_contents($file);
        }
        exit;
    }

}

if (!function_exists('sortListColumn')) {

    function sortListColumn($arrConfig = array())
    {
        if (sizeof($arrConfig)) {
            $url = $arrConfig['url'];
            $classSort = $arrConfig['class'];
            $colCurrent = $arrConfig['colCurrent'];
            $sort = $arrConfig['sort'];
            $page = isset($arrConfig['page']) ? $arrConfig['page'] : 1;
            $arrColumn = $arrConfig['column'];

            $renderHtml = array();
            foreach ($arrColumn as $key => $column) {
                if ($column['name'] == $colCurrent) {
                    if (strtolower($sort) == 'asc') {
                        $linkUrl = $url . '/' . $page . '/' . $column['name'] . '/desc/';
                        $renderHtml[$column['name']] = '<a href="' . $linkUrl . '" class="'
                                . $classSort['classUp'] . '" ><span>' . $column['label'] . '</span></a>';
                    } else {
                        $linkUrl = $url . '/' . $page . '/' . $column['name'] . '/asc/';
                        $renderHtml[$column['name']] = '<a href="' . $linkUrl . '" class="'
                                . $classSort['classDown'] . '"><span>' . $column['label'] . '</span></a>';
                    }
                } else {
                    $linkUrl = $url . '/' . $page . '/' . $column['name'] . '/desc/';
                    $renderHtml[$column['name']] = '<a href="' . $linkUrl . '" class="current"><span>'
                            . $column['label'] . '</span></a>';
                }
            }
            return $renderHtml;
        }
        return null;
    }

}

if (!function_exists('getThumbImage')) {

    function getThumbImage($oldFile, $width, $height, $thumb = TRUE)
    {
        $CI = & get_instance();
        $info = pathinfo($oldFile);
        if ($thumb) {
            $newPath = $info['dirname'] . '/' . $info['filename'] . '_thumb.' . $info['extension'];
            $newFile = $info['filename'] . '_thumb.' . $info['extension'];
        } else {
            $newPath = $info['dirname'] . '/' . $info['filename'] . '_'
                    . $width . 'x' . $height . '.' . $info['extension'];
            $newFile = $info['filename'] . '_' . $width . 'x' . $height . '.' . $info['extension'];
        }

        if (file_exists($newPath) && !empty($newPath)) {
            $infoNew = getimagesize($newPath);
            if (is_array($infoNew) && $infoNew ['0'] == $width && $infoNew ['1'] == $height) {
                return $newFile;
            }
        }

        $config['image_library'] = 'gd2';
        $config['source_image'] = $oldFile;
        $config['create_thumb'] = $thumb;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = $width;
        $config['height'] = $height;

        $CI->load->library('image_lib', $config);
        $CI->image_lib->resize();
        return $newFile;
    }

}